from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.controllers.item_controller import create_item, get_item, update_item, delete_item
from app.database.db_setup import get_db

item_router = APIRouter()


@item_router.post("/items/")
def create_item_route(name: str, description: str, db: Session = Depends(get_db)):
    item = create_item(name, description, db)
    return item


@item_router.get("/items/{item_id}")
def get_item_route(item_id: int, db: Session = Depends(get_db)):
    item = get_item(item_id, db)
    if not item:
        raise HTTPException(status_code=404, detail="Item not found")
    return item


@item_router.put("/items/{item_id}")
def update_item_route(item_id: int, name: str, description: str, db: Session = Depends(get_db)):
    item = update_item(item_id, name, description, db)
    if not item:
        raise HTTPException(status_code=404, detail="Item not found")
    return item


@item_router.delete("/items/{item_id}")
def delete_item_route(item_id: int, db: Session = Depends(get_db)):
    success = delete_item(item_id, db)
    if not success:
        raise HTTPException(status_code=404, detail="Item not found")
    return {"message": "Item deleted successfully"}
    